<template>
  <!-- 获取焦点添加=》类名=》focused -->
  <view class="search" :class="{ focused: isSearch }">
    <!-- 搜索输入区域 -->
    <view class="sinput">
      <input
        type="text"
        v-model="keyword"
        @input="suggestPrd"
        @focus="search"
        @confirm="goSearch"
        placeholder="搜索"
      />
      <button @click="cancelSearch">取消</button>
    </view>
    <!-- 搜索状态显示=》下边内容 -->
    <view class="scontent" v-show="isSearch">
      <view class="title">
        搜索历史
        <text class="clear" @click="clearHistory"></text>
      </view>
      <!-- 搜索历史 -->
      <view class="history" v-if="result.length === 0">
        <navigator
          v-for="item in history"
          :key="item"
          :url="'/packone/list/index?query=' + item"
          >{{ item }}</navigator
        >
      </view>
      <!-- 结果 -->
      <scroll-view scroll-y class="result" v-else>
        <navigator
          v-for="item in result"
          :key="item.goods_id"
          :url="'/packone/goods/index?id=' + item.goods_id"
          >{{ item.goods_name }}</navigator
        >
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 是否是搜索状态
      isSearch: false,
      // 搜索关键词
      keyword: '',
      // 建议商品数据
      result: [],
      // 设置数据=》数据结构：['小米','华为']
      history: uni.getStorageSync('history') || []
    }
  },
  created() {
    console.log('搜索：created');
  },
  // 自定义组件中onLoad不支持的
  // onLoad() {
  //   console.log('搜索：onLoad')
  // },
  methods: {
    // 清空搜索历史记录
    clearHistory() {
      // 1. 内存清空 2. 本地缓存清空
      this.history = []
      uni.removeStorageSync('history')
    },
    // 跳转商品列表页
    goSearch() {
      // 搜索历史记录存储=》排重
      // 存储
      this.history.push(this.keyword)
      // 排重
      this.history = [...new Set(this.history)]
      // 缓存到本地
      uni.setStorageSync('history', this.history)
      // 跳转搜索结果页面
      uni.navigateTo({
        url: '/packone/list/index?query=' + this.keyword
      });
    },
    // 输入的时候展示建议商品
    suggestPrd() {
      // 调用接口查询建议商品数据=》执行频率高=》函数防抖
      this.timer && clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        console.log(this.keyword)
        if (!this.keyword) {
          this.result = []
          return
        }
        const { data } = await this.request({
          url: '/api/public/v1/goods/qsearch', data: {
            query: this.keyword
          }
        })
        console.log(data)
        this.result = data
      }, 600);
    },
    // 获取焦点执行
    search() {
      this.isSearch = true
      // 1、 获取屏幕高度 2. 通知父组件修改高度（不出滚动条）
      const res = uni.getSystemInfoSync();
      this.$emit('search', res.windowHeight + 'px')
      // 隐藏tabBar
      uni.hideTabBar()
    },
    // 取消搜索状态显示
    cancelSearch() {
      this.isSearch = false
      // 页面还可以滚动
      this.$emit('search', 'auto')
      // 清空数据
      this.keyword = ''
      this.result = []
      // 显示tabBar
      uni.showTabBar()
    }
  },
}
</script>

<style lang="scss" scoped>
// 搜索
.search {
  display: flex;
  flex-direction: column;
  .sinput {
    box-sizing: border-box;
    padding: 20rpx 16rpx;
    background: #ff2d4a;
    position: relative;
    //伪元素
    &::after {
      position: absolute;
      top: 28rpx;
      left: 302rpx;
      content: "";
      width: 44rpx;
      height: 44rpx;
      line-height: 1;
      background-image: url(https://static.botue.com/ugo/images/icon_search%402x.png);
      background-size: 32rpx;
      background-position: 6rpx center;
      background-repeat: no-repeat;
    }
    input {
      background: #fff;
      flex: 1;
      height: 60rpx;
      line-height: 60rpx;
      text-align: center;
      font-size: 24rpx;
      color: #bbb;
      border-radius: 5rpx;
    }
    button {
      display: none;
      margin-left: 20rpx;
      width: 150rpx;
      height: 60rpx;
      line-height: 60rpx;
      text-align: center;
      font-size: 24rpx;
      border-radius: 5rpx;
      background: transparent;
      color: #666;
    }
  }
  &.focused {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 10;
    .sinput {
      display: flex;
      background: #eee;
      input {
        text-align: left;
        padding-left: 60rpx;
      }
      button {
        display: block;
      }
      &::after {
        left: 30rpx;
      }
    }
  }
  .scontent {
    background: #fff;
    position: relative;

    flex: 1;
    padding: 27rpx;
    .title {
      font-size: 27rpx;
      line-height: 1;
      color: #333;
    }
    .clear {
      display: block;
      width: 27rpx;
      height: 27rpx;
      float: right;
      background-image: url(http://static.botue.com/ugo/images/clear.png);
      background-size: cover;
    }

    .history {
      padding-top: 30rpx;
      navigator {
        display: inline-block;
        line-height: 1;
        padding: 15rpx 20rpx 12rpx;
        background-color: #ddd;
        font-size: 24rpx;
        margin-right: 20rpx;
        margin-bottom: 15rpx;
        color: #333;
      }
    }

    .result {
      // display: none;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      background-color: #fff;
      navigator {
        line-height: 1;
        padding: 20rpx 30rpx;
        font-size: 24rpx;
        color: #666;
        border-bottom: 1px solid #eee;

        &:last-child {
          border-bottom: none;
        }
      }
    }
  }
}
</style>